package com.burgeon.backend.SHOP.rest.recharger;
/**
 * 查询余额充值记录
 */

import com.burgeon.backend.Rest.base.RESTBase;
import org.json.JSONArray;
import org.json.JSONException;
import org.springframework.util.MultiValueMap;

import java.util.ArrayList;
import java.util.HashMap;

public class FindRECHARGEByPhone extends RESTBase {

    private String ip;
    private String account;
    private String password;

    public FindRECHARGEByPhone(String ip, String account, String password) {
        this.ip = ip;
        this.account = account;
        this.password = password;
    }

    /**
     * 发送并接收字符创
     * @return
     */

    public Double getData(String vip) throws Exception {
        String url = "http://"+ip+"/servlets/binserv/Rest";

        String str = RESULT(Data(vip),url);
        System.err.println(str);

        String code = new JSONArray(str).getJSONObject(0).getString("code");
        String message = new JSONArray(str).getJSONObject(0).getString("message");
        if (!"0".equals(code)){
            throw new Exception(message);
        }
        JSONArray jsonArray = new JSONArray(str.replace("null","0")).getJSONObject(0).getJSONArray("rows");


        double balance;
        try {
            balance = jsonArray.getJSONArray(0).getDouble(0);

        } catch (JSONException e) {
            e.printStackTrace();
            return 0.00;
        }


        return balance;
    }

    private MultiValueMap Data(String code) {
        HashMap params = new HashMap();
        ArrayList columns = new ArrayList();//查询字段


        columns.add("'AMOUNT'");//储值余额

        params.put("columns", columns);
        params.put("table", "FA_VIPACC");//表名

        params.put("count","true");

        HashMap orderby = new HashMap();

        orderby.put("column","NO");
        orderby.put("asc","false");

        HashMap paramss = new HashMap();
        String ac = "'"+ code+"'";
        paramss.put("column", "'C_VIP_ID;CARDNO'");
        paramss.put("condition", ac);
        params.put("params", paramss);

        return DATA("Query", params,account,password);
    }
    public static void main(String[] args) {
        FindRECHARGEByPhone getRetailByVip = new FindRECHARGEByPhone("47.92.53.75:90", "dy@xcx.com", "123");
        try {
            System.err.println(getRetailByVip.getData("17732141036"));

        } catch (Exception e) {
            e.printStackTrace();
        }
    }


}
